package com.neusoft.nep.admin.controller;

import com.neusoft.nep.admin.entity.AqiData;
import com.neusoft.nep.admin.entity.AqiLevelStatistic; // Updated AqiLevelStatistic
import com.neusoft.nep.common.AqiDataFileUtil;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import java.util.HashMap;
import java.util.LinkedHashMap; // Use LinkedHashMap to preserve insertion order
import java.util.List;
import java.util.Map;

public class AQIDistributeTotalStatisViewController {

    @FXML
    private TableView<AqiLevelStatistic> tableView;
    @FXML
    private TableColumn<AqiLevelStatistic, String> degree; // This will now show the numerical level (1, 2, etc.)
    @FXML
    private TableColumn<AqiLevelStatistic, String> describe; // This will show the descriptive text ("优", "良", etc.)
    @FXML
    private TableColumn<AqiLevelStatistic, String> count; // This will show the count

    private ObservableList<AqiLevelStatistic> tableList = FXCollections.observableArrayList();

    // Map for detailed descriptions
    private static final Map<String, String> AQI_DETAILED_DESCRIPTIONS = new HashMap<>();
    static {
        AQI_DETAILED_DESCRIPTIONS.put("优", "空气质量令人满意，基本无空气污染");
        AQI_DETAILED_DESCRIPTIONS.put("良", "空气质量可接受；某些污染物可能对极少数异常敏感人群有弱影响");
        AQI_DETAILED_DESCRIPTIONS.put("轻度污染", "易感人群症状有轻度加剧，健康人群出现刺激症状");
        AQI_DETAILED_DESCRIPTIONS.put("中度污染", "进一步加剧易感人群症状，可能对健康人群也有影响");
        AQI_DETAILED_DESCRIPTIONS.put("重度污染", "每个人都会受到影响，对敏感人群来说非常不健康");
        AQI_DETAILED_DESCRIPTIONS.put("严重污染", "健康人群运动耐受力降低，出现明显强烈症状，提前出现某些疾病");
    }

    // Map to link numerical ID to descriptive level, maintaining order
    private static final Map<String, String> AQI_LEVEL_MAPPING = new LinkedHashMap<>();
    static {
        AQI_LEVEL_MAPPING.put("1", "优");
        AQI_LEVEL_MAPPING.put("2", "良");
        AQI_LEVEL_MAPPING.put("3", "轻度污染");
        AQI_LEVEL_MAPPING.put("4", "中度污染");
        AQI_LEVEL_MAPPING.put("5", "重度污染");
        AQI_LEVEL_MAPPING.put("6", "严重污染");
    }


    @FXML
    public void initialize() {
        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        // Bind columns to the new properties in AqiLevelStatistic
        degree.setCellValueFactory(cellData -> cellData.getValue().idProperty()); // Numerical level
        describe.setCellValueFactory(cellData -> cellData.getValue().levelDescriptionProperty()); // "优", "良" etc.
        count.setCellValueFactory(cellData -> cellData.getValue().countProperty()); // Count

        loadAqiDistributionData();
    }

    private void loadAqiDistributionData() {
        List<AqiData> allAqiData = AqiDataFileUtil.readAqiDataFromFile();

        if (allAqiData.isEmpty()) {
            System.out.println("AQI数据文件为空，无法生成分布统计。");
            // Populate with a default "no data" row using the new constructor
            tableList.add(new AqiLevelStatistic("N/A", "无数据", "文件未包含AQI数据", "0"));
            tableView.setItems(tableList);
            return;
        }

        Map<String, Integer> levelCounts = new HashMap<>();
        for (AqiData data : allAqiData) {
            String level = data.getLevel(); // This is like "优", "良", etc. from AqiData
            if (level != null && !level.trim().isEmpty()) {
                levelCounts.put(level, levelCounts.getOrDefault(level, 0) + 1);
            }
        }

        tableList.clear();

        // Iterate through the ordered numerical levels (1, 2, 3...)
        for (Map.Entry<String, String> entry : AQI_LEVEL_MAPPING.entrySet()) {
            String numericalLevelId = entry.getKey(); // e.g., "1"
            String descriptiveLevel = entry.getValue(); // e.g., "优"

            int countForLevel = levelCounts.getOrDefault(descriptiveLevel, 0); // Get count based on descriptive level
            String detailedDescription = AQI_DETAILED_DESCRIPTIONS.getOrDefault(descriptiveLevel, "无详细描述");

            // Create AqiLevelStatistic with numerical ID, descriptive level, detailed description, and count
            tableList.add(new AqiLevelStatistic(
                    numericalLevelId,
                    descriptiveLevel,
                    detailedDescription,
                    String.valueOf(countForLevel)
            ));
        }

        tableView.setItems(tableList);

        if (tableList.isEmpty()) {
            System.out.println("统计结果为空。");
        }
    }
}